void MaxLike (ImSize, BorderFac, Ch0,Ch1,Ch2, ClasIm, TrainFile)
int   ImSize;
float BorderFac;
BYTE  ** Ch0;
BYTE  ** Ch1;
BYTE  ** Ch2;
BYTE  ** ClasIm;
char  TrainFile[];
{
   int    r,c, y,x, r0,c0, i,NofTrn, WinSize;
   float  M0,M1,M2, CoVar[3][3], CoInv[3][3];
   float  M0tot,M1tot,M2tot, CoVarTot[3][3], CoInvTot[3][3];
   float  MahaSample, Maha;
   FILE   *Stream;

   Stream = fopen (TrainFile, "r");
   fscanf (Stream, "%d%d", &NofTrn, &WinSize);

   M0tot = M1tot = M2tot = (float)0;
   for (y=0; y<3; y++)
      for (x=0; x<3; x++)  CoVarTot[y][x] = CoInvTot[y][x] = (float)0;

   for (i=0; i<NofTrn; i++) {
      fscanf (Stream, "%d%d", &r0,&c0);
      ChanMean  (WinSize, r0,c0, Ch0,Ch1,Ch2, &M0,&M1,&M2);
      ChanCoVar (WinSize, r0,c0, Ch0,Ch1,Ch2, M0,M1,M2, CoVar);
      InvCoVar  (CoVar,CoInv);
      M0tot += M0;
      M1tot += M1;
      M2tot += M2;
      for (y=0; y<3; y++) {
         for (x=0; x<3; x++) {
            CoVarTot[y][x] += CoVar[y][x];
            CoInvTot[y][x] += CoInv[y][x];
   }  }  }

   M0tot /= NofTrn;
   M1tot /= NofTrn;
   M2tot /= NofTrn;
   for (y=0; y<3; y++) {
      for (x=0; x<3; x++) {
         CoVarTot[y][x] /= NofTrn;
         CoInvTot[y][x] /= NofTrn;
   }  }

   MahaSample = MahaDist (BorderFac * sqrt(CoVarTot[0][0]),
                          BorderFac * sqrt(CoVarTot[1][1]),
                          BorderFac * sqrt(CoVarTot[2][2]),
                          CoInvTot);

   for (r=0; r<ImSize; r++) {
      for (c=0; c<ImSize; c++) {
         Maha = MahaDist (Ch0[r][c] - M0tot,
                          Ch1[r][c] - M1tot,
                          Ch2[r][c] - M2tot,
                          CoInvTot);
         if (Maha < MahaSample)  ClasIm[r][c] = 255;
                           else  ClasIm[r][c] = 0;
}  }  }
